/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.amse.grigory.dchess.player;

import org.amse.grigory.dchess.kernel.*;
import org.amse.grigory.dchess.rules.*;
import org.amse.grigory.dchess.view.*;

/**
 *
 * @author grigory
 */
public class GameControler {

    private IModel myModel;
    private IPlayer myWhite,  myBlack;
    private boolean myEnabled = true;
    private boolean myUndo = false;

    public GameControler(IModel model, IPlayer white, IPlayer black) {
        myModel = model;
        myWhite = white;
        myBlack = black;
    }

    public void start() {       
        new GameThread().start();        
    }

    public void kill() {
        myEnabled = false;
    }

    public IPlayer getWhitePlayer() {
        return myWhite;
    }

    public IPlayer getBlackPlayer() {
        return myBlack;
    }

    public void undo() {
        myUndo = true;
        synchronized (myBlack.lock()) {
            synchronized (myWhite.lock()) {
                myModel.undo();
                myWhite.lock().notifyAll();
            }
            myBlack.lock().notifyAll();
        }
    }

    public void redo() {
        myUndo = true;
        synchronized (myBlack.lock()) {
            synchronized (myWhite.lock()) {
                myModel.redo();
                myWhite.lock().notifyAll();
            }
            myBlack.lock().notifyAll();
        }
    }

    private class GameThread extends Thread {

        @Override
        public void run() {
            IPlayer player = null;
            if (myModel.getColor()==Rules.COLOR_WHITE){
                player= myWhite;
            }else{
                player= myBlack;
            }
            while (myModel.isWin() == Rules.NOBODY_WIN) {
                player.calc();
                if (!player.moved()) {
                    synchronized (player.lock()) {
                        if (!player.moved()) {
                            try {
                                player.lock().wait();
                            } catch (InterruptedException ex) {
                            }
                        }
                    }
                }
                if (myUndo) {
                    myUndo = false;
                    if (myModel.getColor() == Rules.COLOR_WHITE) {
                        player = myWhite;
                    } else {
                        player = myBlack;
                    }
                } else {
                    int sx = player.getSX();
                    int sy = player.getSY();
                    int x = player.getX();
                    int y = player.getY();
                    while (myModel.isPaused()) {
                    }
                    if (!myEnabled) {
                        return;
                    }
                    myModel.makeMove(sx, sy, x, y);
                    if (player == myWhite) {
                        player = myBlack;
                    } else {
                        player = myWhite;
                    }
                }
            }

        }
    }
}
